// BEGIN CUT HERE

// END CUT HERE
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
#include <utility>
using namespace std;

#define ALL(v) (v).begin(), (v).end()
#define LET(x,a) typeof(a) x(a)
#define FOR(i,a,n) for(LET(i,a);i!=n;i++)
#define REP(i,n) FOR(i,0,n)
#define sz size()
#define pb push_back
#define LL long long
#define VI vector<int>
#define VII vector< VI >
#define VS vector<string>
#define PRINT(x) REP(i,x.sz) cout<<x[i]<<" "; cout<<endl;
#define GI ({int a; cin>>a; a;})
#define COUNT(f,x) ({int _=0;f _+=(x);_;})
#define MIN(f,x) ({LL _=LINF;f _<?=(x);_;})
#define MAX(f,x) (-MIN(f,-(x)))



// BEGIN CUT HERE
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

template<typename T> void print( T a ) {
  cerr << a;
}
static void print( long long a ) {
  cerr << a << "L";
}
static void print( string a ) {
  cerr << '"' << a << '"';
}
template<typename T> void print( vector<T> a ) {
  cerr << "{";
  for ( int i = 0 ; i != a.size() ; i++ ) {
    if ( i != 0 ) cerr << ", ";
    print( a[i] );
  }
  cerr << "}" << endl;
}
template<typename T> void eq( int n, T have, T need ) {
  if ( have == need ) {
    cerr << "Case " << n << " passed." << endl;
  } else {
    cerr << "Case " << n << " failed: expected ";
    print( need );
    cerr << " received ";
    print( have );
    cerr << "." << endl;
  }
}
template<typename T> void eq( int n, vector<T> have, vector<T> need ) {
  if( have.size() != need.size() ) {
    cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
    print( have );
    print( need );
    return;
  }
  for( int i= 0; i < have.size(); i++ ) {
    if( have[i] != need[i] ) {
      cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
      print( have );
      print( need );
      return;
    }
  }
  cerr << "Case " << n << " passed." << endl;
}
static void eq( int n, string have, string need ) {
  if ( have == need ) {
    cerr << "Case " << n << " passed." << endl;
  } else {
    cerr << "Case " << n << " failed: expected ";
    print( need );
    cerr << " received ";
    print( have );
    cerr << "." << endl;
  }
}
// END CUT HERE
class CardsCheating {
public:
  int numberOfShuffles(vector <int> cards, vector <int> shuffle);


};

set<VI> poss;

VI doShuffle(VI &cards , VI &shuffle){
  VI ret(cards.sz) ;
  REP(i,shuffle.sz) ret[shuffle[i]] = cards[i];
  return ret;
}

int CardsCheating::numberOfShuffles(vector <int> cards, vector <int> shuffle) {
  int res=0;
  VI fin(cards.sz); 
  REP(i,fin.sz) fin[i] = i%3;
  while( cards != fin ){
    res++;
    if( poss.find(cards) == poss.end() ) poss.insert(cards);
    else return -1;
    cards = doShuffle(cards,shuffle);
  }
 
  return res;
}
// BEGIN CUT HERE
int main( int argc, char* argv[] ) {
  {
    int cardsARRAY[] = {0,1,2,0,1,2};
    vector <int> cards( cardsARRAY, cardsARRAY+ARRSIZE(cardsARRAY) );
    int shuffleARRAY[] = {1,4,0,3,2,5};
    vector <int> shuffle( shuffleARRAY, shuffleARRAY+ARRSIZE(shuffleARRAY) );
    CardsCheating theObject;
    eq(0, theObject.numberOfShuffles(cards, shuffle),0);
  }
  {
    int cardsARRAY[] = {2,0,1};
    vector <int> cards( cardsARRAY, cardsARRAY+ARRSIZE(cardsARRAY) );
    int shuffleARRAY[] = {1,2,0};
    vector <int> shuffle( shuffleARRAY, shuffleARRAY+ARRSIZE(shuffleARRAY) );
    CardsCheating theObject;
    eq(1, theObject.numberOfShuffles(cards, shuffle),2);
  }
  {
    int cardsARRAY[] = {1,1,2,0,2,0,1,0,2,2,1,0};
    vector <int> cards( cardsARRAY, cardsARRAY+ARRSIZE(cardsARRAY) );
    int shuffleARRAY[] = {5,0,9,7,1,8,3,10,4,11,6,2};
    vector <int> shuffle( shuffleARRAY, shuffleARRAY+ARRSIZE(shuffleARRAY) );
    CardsCheating theObject;
    eq(2, theObject.numberOfShuffles(cards, shuffle),59);
  }
  {
    int cardsARRAY[] = {1,0,2};
    vector <int> cards( cardsARRAY, cardsARRAY+ARRSIZE(cardsARRAY) );
    int shuffleARRAY[] = {0,2,1};
    vector <int> shuffle( shuffleARRAY, shuffleARRAY+ARRSIZE(shuffleARRAY) );
    CardsCheating theObject;
    eq(3, theObject.numberOfShuffles(cards, shuffle),-1);
  }
}
// END CUT HERE
